<?php
/*
 * данный класс реализует задачу перенаправления на другие контроллере
 * имеет в себе реализацию паттерна синглтон и обращение к паттерну
 * фабрика, реализованном в контроллере Fabric.php
 */
class Control_FrontAction {

    private static $frontObj = null;
    private $params;
    private $post;
    private $view;

    public function __construct() {
        $this->params = $_GET;
        $this->post   = $_POST;
        $this->view   = new View_View();
    }

    public static function single() {
        if (null == self::$frontObj) {
            self::$frontObj = new Control_FrontAction();
        }
        return self::$frontObj;
    }
    public function run() {
        session_start();
        if (empty($this->params['control'])) {
            $actionController = Control_Fabric::getControl('Index', $this->view, $this->params);
        } else {
            switch ($this->params['control']) {
                case 'reg':
                    $actionController = Control_Fabric::getControl('Registration', $this->view, $this->post);
                    break;
                case 'auth':
                    $actionController = Control_Fabric::getControl('Auth', $this->view, $this->post);
                    break;
                case 'user':
                    $actionController = Control_Fabric::getControl('User', $this->view, $this->params);
                    break;
                case 'search':
                    foreach($this->params as $k  => $v){
                        $this->post['page'] = $k; 
                    }
                    $actionController = Control_Fabric::getControl('Search', $this->view, $this->post);
                    break;
                case 'userRedact':
                    foreach($this->params as $k  => $v){
                        $this->post['id'] = $k; 
                    }
                    $actionController = Control_Fabric::getControl('UserRedact', $this->view, $this->post);
                    break;
                case 'send':
                    $actionController = Control_Fabric::getControl('Send', $this->view, $this->post);
                    break;
                case 'upload':
                    $actionController = Control_Fabric::getControl('Upload', $this->view, $this->post);
                    break;
                case 'photo':
                    $actionController = Control_Fabric::getControl('Photo', $this->view, $this->params);
                    break;
                case 'ava':
                    $actionController = Control_Fabric::getControl('Ava', $this->view, $this->params);
                    break;
                case 'logout':
                    $_SESSION['authUserId'] = null;
                default:
                    $actionController = Control_Fabric::getControl('Index', $this->view);
            }
        }
        $actionController->run();
        $this->view->render('main');
    }
    

}
